import {
  getAppointmentShopDetail,
  getOrderDetail,
  getOrderList,
} from "@src/api/reservation";
import {
  STATE_TEXT,
  RESERVATION_STATE,
  RESERVATION_PAY_STATE,
  RESERVATION_PAY_STATE_TEXT,
  RESERVATION_ORDER_STATE,
  ORDER_SUB_STATUS,
  RESERVATION_PAY_TYPE,
} from "./config/index";
import router from "@src/router";
import {
  getPayChannels,
  getWriteOffQrCode,
  handleCheckAllowedPwd,
} from "@src/common/reservation/config/index.js";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import payBehavior from "./payBehavior";
import reservation from "@src/store/reservation";
import { debounce } from "@src/common/utils";
const STATE_CORLOR = {
  0: "#47B716",
  1: "#E64656",
  2: "#E64656",
  3: "#E64656",
};

Component({
  behaviors: [storeBindingsBehavior, payBehavior],
  storeBindings: [
    {
      store: reservation,
      fields: ["goodsInfo"],
      actions: ["setSubmitGoodsInfo"],
    },
  ],
  pageLifetimes: {
    show: function () {
      // 页面被展示
      console.log("页面被展示");
      this.getList(true);
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    },
  },
  properties: {},
  data: {
    STATE_TEXT,
    STATE_CORLOR,
    RESERVATION_STATE,
    RESERVATION_PAY_STATE,
    RESERVATION_PAY_STATE_TEXT,
    RESERVATION_ORDER_STATE,
    ORDER_SUB_STATUS,
    RESERVATION_PAY_TYPE,
    list: [],
    hasNextPage: true,
    params: {
      pageSize: 10,
      pageNum: 1,
    },
    qrUrl: "",
    qrcodeWriteOffCode: "",
    isShowQRPopup: false,
    payChannels: [],
    originOffsetY: 0,
  },
  ready() {
    this.getList(true);
  },
  methods: {
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.setData({ pageNum: 1, pageSize: 10, hasNextPage: true });
      this.getList(true);
    },
    onReachBottom() {
      if (this.data.hasNextPage) {
        this.handleToLower();
      }
    },
    handleToShopHome(e) {
      const {
        currentTarget: {
          dataset: {
            item: { id },
          },
        },
      } = e;
      router.push({
        name: "reservation/home",
        params: {
          shopId: id,
        },
      });
    },
    async getList(isClear = false) {
      if (isClear) {
        this.setData({
          params: {
            ...this.data.params,
            pageSize: 10,
            pageNum: 1,
          },
        });
      }
      const { code, data } = await getOrderList(this.data.params);
      if (code === 0) {
        wx.stopPullDownRefresh();
        const { hasNextPage, list } = data;
        this.setData({
          hasNextPage,
          list: isClear ? list : [...this.data.list, ...list],
        });
      }
    },
    handleToLower() {
      // if (!this.data.hasNextPage) return;
      const pageNum = this.data.params.pageNum + 1;
      this.setData({
        params: {
          ...this.data.params,
          pageNum,
        },
      });
      this.getList(false);
    },
    handleToDetail(e) {
      const {
        currentTarget: {
          dataset: { item },
        },
      } = e;
      console.log(item);
      router.push({
        name: "reservation/reservation_order_detail",
        params: {
          title: 1,
          id: item.id,
          shopId: item.shopId,
          orderNo: item.orderNo,
          // navIndex: e.currentTarget.dataset.index,
        },
      });
    },
    handleWriteOff(e) {
      wx.showLoading({
        title: "加载核销码中",
      });
      const {
        currentTarget: {
          dataset: { item },
        },
      } = e;
      const writeOffCode = item.writeOffCode;
      console.log("writeOffCode", writeOffCode);
      if (!writeOffCode) {
        wx.hideLoading();
        return;
      }
      getWriteOffQrCode(writeOffCode).then((qrUrl) => {
        wx.hideLoading();
        if (qrUrl) {
          this.setData({
            qrUrl,
            qrcodeWriteOffCode: item.writeOffCode,
            isShowQRPopup: true,
          });
        }
      });
    },
    async handlePay(e) {
      const {
        currentTarget: {
          dataset: { item },
        },
      } = e;
      router.redirectTo({
        name: "reservation/reservation_order_detail",
        params: {
          ...item,
        },
      });
      // console.log(item)
      // const payChannels = getPayChannels(item.payChannels);
      // this.setData({
      //   payChannels,
      // });
      // const {
      //   orderNo,
      //   orderVo: { payFee },
      // } = item;
      // //新增初始化会员密码操作
      // const { accountPayRule } = this.data;
      // const initialed = await handleCheckAllowedPwd(payFee, accountPayRule);
      // if (!initialed) return;
      // this.toPay({
      //   orderNo,
      //   payFee,
      //   maxPayLatency: 300,
      // })
      //   .then(() => {
      //     router.redirectTo({
      //       // name: "reservation/reservation_order_detail",
      //       name: "reservation/reservation_success",
      //       params: {
      //         ...item,
      //       },
      //     });
      //     // this.getList(true);
      //   })
      //   .catch(() => {
      //     this.getList(true);
      //   });
    },
    handleCloseQRCode() {
      this.setData({
        isShowQRPopup: false,
      });
    },
  },
});
